package com.managedBean.cart;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import com.controller.cart.CheckOutController;
import com.dto.cart.CartOrderDTO;
import com.managedBean.registration.LoginManagedBean;
import com.managedBean.registration.RegistrationManagedBean;
import com.utils.JsfUtils;

@ManagedBean(name = "checkOut")
@SessionScoped
public class CheckOutManagedBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1336723770882028146L;
	private int orderId;
	private CartManagedBean cartManagedBean;
	private RegistrationManagedBean registrationManagedBean;
	LoginManagedBean loginBean;
	// private transient OrderDAO dao;
	@ManagedProperty(value = "#{checkOutController}")
	private CheckOutController checkOutController;

	public CheckOutManagedBean() {

	}

	@PostConstruct
	public void init() {
		cartManagedBean = (CartManagedBean) JsfUtils
				.getBeanInstanceFromFacesContext("cart");
		registrationManagedBean = (RegistrationManagedBean) JsfUtils
				.getBeanInstanceFromFacesContext("registration");
		loginBean = (LoginManagedBean) JsfUtils
				.getBeanInstanceFromFacesContext("login");
		// dao = new OrderDAO();
		if(null!=loginBean && null!=loginBean.getUserDTO()){
		registrationManagedBean.setName(loginBean.getUserDTO().getName());
		registrationManagedBean.setMobileNo(loginBean.getUserDTO()
				.getMobileno());
		registrationManagedBean.setEmail(loginBean.getUserDTO().getEmail());
		registrationManagedBean.setHouseNo(loginBean.getUserDTO().getHouseno());
		registrationManagedBean.setStreet(loginBean.getUserDTO().getStreet());
		registrationManagedBean.setArea(loginBean.getUserDTO().getArea());
		registrationManagedBean.setCity(loginBean.getUserDTO().getCity());
		registrationManagedBean.setLandmark(loginBean.getUserDTO()
				.getLandmark());
		registrationManagedBean.setShowRegistrationDetailsFlag(true);
		registrationManagedBean.setIduserTable(loginBean.getUserDTO()
				.getIduserTable());
		registrationManagedBean.setChkOutType("");
		}
	}

	public void createOrder() {
		try {
			CartOrderDTO dto = new CartOrderDTO();
			dto.setItemsList(cartManagedBean.getCartItemsList());
			dto.setShippingCharges(cartManagedBean.getShippingCharges());
			dto.setSubTotal(cartManagedBean.getSubTotal());
			dto.setTotal(cartManagedBean.getTotal());
			dto.setUserId(registrationManagedBean.getIduserTable());
			String checkOutType;
			if(registrationManagedBean.getChkOutType().equalsIgnoreCase("New User")){
				checkOutType="N";
			}else if(registrationManagedBean.getChkOutType().equalsIgnoreCase("Guest")){
				checkOutType="G";
			}else {
				checkOutType="E";
			}
			dto.setOrderType(checkOutType);
			orderId = checkOutController.createOrder(dto);
			//TODO send mail with order id
			PrepareMail mail=new PrepareMail();
			mail.sendMail(dto,orderId);
			
			// orderId = dao.createOrder(cartManagedBean.getCartItemsList());
			JsfUtils.navigateTo("/pages/ui/orderConfirm.jsf?faces-redirect=true");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @return the orderId
	 */
	public int getOrderId() {
		return orderId;
	}

	/**
	 * @param orderId
	 *            the orderId to set
	 */
	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}

	/**
	 * @return the cartManagedBean
	 */
	public CartManagedBean getCartManagedBean() {
		return cartManagedBean;
	}

	/**
	 * @param cartManagedBean
	 *            the cartManagedBean to set
	 */
	public void setCartManagedBean(CartManagedBean cartManagedBean) {
		this.cartManagedBean = cartManagedBean;
	}

	/**
	 * @return the registrationManagedBean
	 */
	public RegistrationManagedBean getRegistrationManagedBean() {
		return registrationManagedBean;
	}

	/**
	 * @param registrationManagedBean
	 *            the registrationManagedBean to set
	 */
	public void setRegistrationManagedBean(
			RegistrationManagedBean registrationManagedBean) {
		this.registrationManagedBean = registrationManagedBean;
	}

	/**
	 * @return the checkOutController
	 */
	public CheckOutController getCheckOutController() {
		return checkOutController;
	}

	/**
	 * @param checkOutController
	 *            the checkOutController to set
	 */
	public void setCheckOutController(CheckOutController checkOutController) {
		this.checkOutController = checkOutController;
	}

}
